package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"sync/atomic"
	"time"
)

var x int64 = 6

func storeFunc() {
	for i := 0; ; i++ {
		if i%2 == 0 {
			x = 66
		} else {
			x = 666
		}
	}
}

var license, registerServer string

func printCheckEnvUsage() {
	fmt.Println("checkenv will check if the hardware and software meet the requirement")
}
func printInstallUsage() {
	fmt.Println("printInstallUsage will check if the hardware and software meet the requirement")
}
func printUninstallUsage() {
	fmt.Println("printUninstallUsage will check if the hardware and software meet the requirement")
}
func main() {
	fmt.Println("Hello World kso KSOFuncs ")
	f := CreateFile("C:/ex.txt")
	writeFile(f)
	defer closeFile(f)
	//functest1main()
	//functest23()
	//mainsdd()
	////functest24()
	//functest25()
	//functest26()
	//functestmain()
	//functest4main()
	checkEnvCmd := flag.NewFlagSet("checkenv", flag.ExitOnError)
	checkEnvCmd.Usage = printCheckEnvUsage

	installCmd := flag.NewFlagSet("install", flag.ExitOnError)
	installCmd.StringVar(&license, "license", "", "The Volume License")
	installCmd.StringVar(&registerServer, "server", "http://mx-us-nc-1.tvunetworks.com:5123", "The Volume License Register Server")
	installCmd.Usage = printInstallUsage

	uninstallCmd := flag.NewFlagSet("uninstall", flag.ExitOnError)
	uninstallCmd.StringVar(&registerServer, "server", "http://mx-us-nc-1.tvunetworks.com:5123", "The Volume License Register Server")
	uninstallCmd.Usage = printUninstallUsage

	flag.Usage = printUsage
	flag.Parse()
	fmt.Println("license:", license, "  , server:", registerServer)
	if len(os.Args) < 2 {
		printUsage()
		os.Exit(1)
	}
	switch os.Args[1] {
	case "version":
		printVersion()
	case "checkenv":
		if !CheckIfUserIsRoot() {
			fmt.Println("Please run this application with root or sudo")
			os.Exit(1)
		}
		checkEnvCmd.Parse(os.Args[2:])
	case "checklicense":
		if !CheckIfUserIsRoot() {
			fmt.Println("Please run this application with root or sudo")
			os.Exit(1)
		}
	case "install":
		if !CheckIfUserIsRoot() {
			fmt.Println("Please run this application with root or sudo")
			os.Exit(1)
		}
		installCmd.Parse(os.Args[2:])
	case "uninstall":
		if !CheckIfUserIsRoot() {
			fmt.Println("Please run this application with root or sudo")
			os.Exit(1)
		}
		uninstallCmd.Parse(os.Args[2:])
	case "upgrade":
		installCmd.Parse(os.Args[2:])
	case "imageOperation":
		installCmd.Parse(os.Args[2:])
	default:
		printUsage()
		os.Exit(1)
	}
}
func CheckIfUserIsRoot() bool {
	if output, err := exec.Command("id", "-u").Output(); err != nil {
		fmt.Println("Cannot verify the current user. Fatal error. Will now exit")
		os.Exit(1)
	} else {
		if i, err := strconv.Atoi(string(output[:len(output)-1])); err != nil {
			fmt.Println("Cannot verify the current user. Fatal error. Will now exit")
			os.Exit(1)
		} else {
			return i == 0
		}
	}

	return false
}

var AGENTCTL_VERSION string = "0.3.0"

func printVersion() {
	fmt.Println(AGENTCTL_VERSION)
}
func printUsage() {
	fmt.Println(`Usage:
    version
    checkenv
    checklicense
    install
    uninstall
    upgrade
    imageOperation`)
}
func msd() {
	go storeFunc()
	go func() {
		for {
			time.Sleep(time.Second)
			zz := atomic.LoadInt64(&x)
			fmt.Printf("xxxxxxxxx1:%x\n", zz)
		}
	}()
	go storeFunc1()
	for {
		time.Sleep(1000 * time.Millisecond)
		z := atomic.LoadInt64(&xx)
		fmt.Printf("xxxxxxxxx2222:%x\n", z)
	}
}

var xx int64 = 1

func storeFunc1() {
	for i := 0; ; i++ {
		if i%2 == 0 {
			atomic.StoreInt64(&xx, 2)
		} else {
			atomic.StoreInt64(&xx, 3)
		}
	}
}

func CreateFile(p string) *os.File {
	fmt.Println("creating...")
	create, err := os.Create(p)
	if err != nil {
		panic(err)
	}
	fmt.Println("creating...")
	return create
}
func writeFile(f *os.File) {
	fmt.Println("writing...")
	fmt.Fprintf(f, "datasd kso !!!!")
}
func closeFile(f *os.File) {
	fmt.Println("closing...")
	err := f.Close()
	if err != nil {
		panic(err)
		os.Exit(1)
	}
}
